package com.example.kp.mci;

import android.content.Context;

import com.example.kp.mci.constants.options;

public class PreferencesManager {

	private static PreferencesManager instance;
	private Context context;

	public static PreferencesManager getInstance() {
		if (instance == null) {
			instance = new PreferencesManager();
		}
		return instance;
	}

	private PreferencesManager() {
		initializeDatabaseValues();
	}

	private String userProfile;
	private int fontSize;
	private int font;
	private int lineSpacing;
	private int colorTheme;

	private boolean poiListOn;
	private boolean speechOutputOn;
	private int volume;
	private int speed;
	private boolean subtitlesOn;

	private boolean speechInputOn;
	private boolean feedbackOn;
	private boolean speechFeedbackOn_subtitleFeedbackOff;

	DatabaseManagement dbm = DatabaseManagement.getInstance();
	private boolean updateDB = true;

	public String getUserProfile() {
		return userProfile;
	}

	public int getFontSize() {
		return fontSize;
	}

	public int getFont() {
		return font;
	}

	public int getLineSpacing() {
		return lineSpacing;
	}

	public int getColorTheme() {
		return colorTheme;
	}

	public boolean isPoiListOn() {
		return poiListOn;
	}

	public boolean isSpeechOutputOn() {
		return speechOutputOn;
	}

	public int getVolume() {
		return volume;
	}

	public int getSpeed() {
		return speed;
	}

	public boolean isSubtitlesOn() {
		return subtitlesOn;
	}

	public boolean isSpeechInputOn() {
		return speechInputOn;
	}

	public boolean isFeedbackOn() {
		return feedbackOn;
	}

	public boolean isSpeechFeedbackOn_subtitleFeedbackOff() {
		return speechFeedbackOn_subtitleFeedbackOff;
	}

	public void setUserProfile(String profile) {
		this.userProfile = profile;
		notifyOptionChanged(options.USER_PROFILE);
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
		notifyOptionChanged(options.FONT_SIZE);
	}

	public void setFont(int font) {
		this.font = font;
		notifyOptionChanged(options.FONT);
	}

	public void setLineSpacing(int lineSpacing) {
		this.lineSpacing = lineSpacing;
		notifyOptionChanged(options.LINE_SPACING);
	}

	public void setColorTheme(int colorTheme) {
		this.colorTheme = colorTheme;
		notifyOptionChanged(options.COLOR_THEME);
	}

	public void setPoiListOn(boolean poiListOn) {
		this.poiListOn = poiListOn;
		notifyOptionChanged(options.POI_LIST_ON);
	}

	public void setSpeechOutputOn(boolean speechOutputOn) {
		this.speechOutputOn = speechOutputOn;
		notifyOptionChanged(options.SPEECH_OUTPUT_ON);
	}

	public void setVolume(int volume) {
		this.volume = volume;
		notifyOptionChanged(options.VOLUME);
	}

	public void setSpeed(int speed) {
		this.speed = speed;
		notifyOptionChanged(options.SPEED);
	}

	public void setSubtitlesOn(boolean subtitlesOn) {
		this.subtitlesOn = subtitlesOn;
		notifyOptionChanged(options.SUBTITLES_ON);
	}

	public void setSpeechInputOn(boolean speechInputOn) {
		this.speechInputOn = speechInputOn;
		notifyOptionChanged(options.SPEECH_INPUT_ON);
	}

	public void setFeedbackOn(boolean feedbackOn) {
		this.feedbackOn = feedbackOn;
		notifyOptionChanged(options.FEEDBACK_ON);
	}

	public void setSpeechFeedbackOn_subtitleFeedbackOff(
			boolean speechFeedbackOn_subtitleFeedbackOff) {
		this.speechFeedbackOn_subtitleFeedbackOff = speechFeedbackOn_subtitleFeedbackOff;
		notifyOptionChanged(options.SPEECH_FEEDBACK_ON__SUBTITLE_FEEDBACK_OFF);
	}

	private void notifyOptionChanged(int option) {
		// Log.d("PREFERENCES_MANAGER::OPTION CHANGED", "OPTION CODE: " +
		// option);
		// Log.d("PREFERENCES: ", toString());// remove
		if (updateDB) {
			dbm.insertSettings(userProfile, fontSize, font, lineSpacing,
					colorTheme, speechOutputOn, volume, speed, subtitlesOn,
					speechInputOn, feedbackOn,
					speechFeedbackOn_subtitleFeedbackOff, poiListOn);
		}
	}

	public void initializeDefaultDummies() {

		setFontSize(10);
		setFont(com.example.kp.mci.constants.font.NORMAL_SANS_SERIF);
		setLineSpacing(com.example.kp.mci.constants.lineSpacing.NORMAL_1_0);
		setColorTheme(com.example.kp.mci.constants.colorTheme.NORMAL);

		setSpeechOutputOn(false);
		setVolume(80);
		setSpeed(100);
		setSubtitlesOn(false);

		setSpeechInputOn(false);
		setFeedbackOn(false);
		setSpeechFeedbackOn_subtitleFeedbackOff(true);

	}

	public void initializeDatabaseValues() {
		updateDB = false;
		User user = dbm.selectFirstSettings();
		setFontSize(user.getFontSize());
		setFont(user.getFont());
		setLineSpacing(user.getLineSpacing());
		setColorTheme(user.getColorTheme());

		setSpeechOutputOn(user.isSpeechOutputOn());
		setVolume(user.getVolume());
		setSpeed(user.getSpeed());
		setSubtitlesOn(user.isSubtitlesOn());

		setSpeechInputOn(user.isSpeechInputOn());
		setFeedbackOn(user.isFeedbackOn());
		setSpeechFeedbackOn_subtitleFeedbackOff(user
				.isSpeechFeedbackOn_subtitleFeedbackOff());
		setPoiListOn(user.isPoiListOn());
		updateDB = true;
	}

	public String toString() {

		return "Prefs: " + " userProfile: " + userProfile + " fontSize: "
				+ fontSize + " font: " + font + " lineSpacing: " + lineSpacing
				+ " colorTheme: " + colorTheme + " poiListOn: " + poiListOn
				+ " speechOutputOn: " + speechOutputOn + " volume: " + volume
				+ " speed: " + speed + " subtitlesOn: " + subtitlesOn +

				" speechInputOn: " + speechInputOn + " feedbackOn: "
				+ feedbackOn + " speechFeedbackOn_subtitleFeedbackOff: "
				+ speechFeedbackOn_subtitleFeedbackOff;

	}

	public boolean isUpdateDB() {
		return updateDB;
	}

	public void setUpdateDB(boolean updateDB) {
		this.updateDB = updateDB;
	}

}
